package com.zshan.clinic.admin.service.config;

import com.zshan.clinic.admin.entity.vo.MedicalFieldGroupVo;
import com.zshan.clinic.admin.entity.vo.MedicalFieldSupportTreeVo;
import com.zshan.clinic.admin.entity.vo.MedicalFieldVo;
import com.zshan.clinic.admin.mapper.config.SpecMedicalFieldMapper;
import com.zshan.clinic.common.constant.CommonConstant;
import com.zshan.clinic.common.enums.FieldScopeEnum;
import com.zshan.clinic.common.enums.FieldStyleEnum;
import com.zshan.clinic.common.enums.FieldTypeEnum;
import com.zshan.clinic.common.enums.YesOrNoEnum;
import com.zshan.clinic.common.exception.BusinessFailException;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.common.util.page.PageUtils;
import com.zshan.clinic.common.util.string.StringUtil;
import com.zshan.clinic.common.util.tree.TreeParser;
import com.zshan.clinic.database.dao.MedicalFieldMapper;
import com.zshan.clinic.database.dao.MedicalFieldSupportMapper;
import com.zshan.clinic.database.model.MedicalField;
import com.zshan.clinic.database.model.MedicalFieldExample;
import com.zshan.clinic.database.model.MedicalFieldSupport;
import com.zshan.clinic.database.model.MedicalFieldSupportExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 病历字段设置
 */
@Service
@Slf4j
public class MedicalFieldService {

    /**
     * 病历字段  主诉
     */
    @Autowired
    private MedicalFieldMapper medicalFieldMapper;

    /**
     * 病历字段值 围产情况，出生情况
     */
    @Autowired
    private MedicalFieldSupportMapper medicalFieldSupportMapper;

    @Autowired
    private SpecMedicalFieldMapper specMedicalFieldMapper;


    /**
     * 查询字段列表
     * @param fieldIdList
     * @return
     */
    public List<MedicalField> getFieldListByIdList(List<Long> fieldIdList){
        MedicalFieldExample example = new MedicalFieldExample();
        MedicalFieldExample.Criteria criteria = example.createCriteria();
        criteria.andFieldIdIn(fieldIdList);
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        return medicalFieldMapper.selectByExample(example);
    }

    /**
     * 查询必须的字段
     * @return
     */
    public List<MedicalField> getRequiredFieldList(){
        MedicalFieldExample example = new MedicalFieldExample();
        MedicalFieldExample.Criteria criteria = example.createCriteria();
        criteria.andIsRequiredEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        return medicalFieldMapper.selectByExample(example);
    }
    /**
     * 查询默认的病历字段
     * @return
     */
    public List<MedicalField> getDefaultFieldList(){
        MedicalFieldExample example = new MedicalFieldExample();
        MedicalFieldExample.Criteria criteria = example.createCriteria();
        criteria.andFieldScopeIn(FieldScopeEnum.getWxList());
        criteria.andIsDefaultEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        return medicalFieldMapper.selectByExample(example);
    }

    /**
     * 根据ID集合查询字段列表
     * @param fieldIdList
     * @return
     */
    public List<MedicalField> getMedicalFieldListByIds(List<Long> fieldIdList) {
        MedicalFieldExample example = new MedicalFieldExample();
        MedicalFieldExample.Criteria criteria = example.createCriteria();
        criteria.andFieldIdIn(fieldIdList);
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        return medicalFieldMapper.selectByExample(example);
    }

    /**
     * 获取全部的病历字段
     * @return
     */
    public MedicalFieldGroupVo getAllFieldList() {
        List<MedicalFieldVo> fieldVoList = getMedicalFieldList(null,null);
        if(CollectionUtils.isEmpty(fieldVoList)){
            return MedicalFieldGroupVo.build();
        }
        MedicalFieldGroupVo medicalAllFieldVo = new MedicalFieldGroupVo();
        medicalAllFieldVo.setWesternList(fieldVoList.stream()
                .filter(f -> FieldScopeEnum.ALL.getValue().equals(f.getFieldScope()) || FieldScopeEnum.WM.getValue().equals(f.getFieldScope()))
                .collect(Collectors.toList()));
        medicalAllFieldVo.setChineseList(fieldVoList.stream()
                .filter(f -> FieldScopeEnum.ALL.getValue().equals(f.getFieldScope()) || FieldScopeEnum.TCM.getValue().equals(f.getFieldScope()))
                .collect(Collectors.toList()));
        return medicalAllFieldVo;
    }

    /**
     * 获取病历字段
     * @param fieldScope
     * @param filedList
     * @return
     */
    public List<MedicalFieldVo> getMedicalFieldList(String fieldScope,List<String> filedList){
        List<MedicalFieldVo> medicalFieldVoList = specMedicalFieldMapper.getMedicalFieldList(fieldScope,filedList);
        if(CollectionUtils.isEmpty(medicalFieldVoList)){
            return Collections.emptyList();
        }
        medicalFieldVoList.forEach(item -> item.setTreeList(support2Tree(item.getSupportList())));
        return medicalFieldVoList;
    }


    /**
     * 获取字段支持树
     * @param fieldId
     * @return
     */
    public List<MedicalFieldSupportTreeVo> getFieldSupportTree(Long fieldId) {
        List<MedicalFieldSupport> supportList = getSupportList(fieldId);
        if(CollectionUtils.isEmpty(supportList)){
            return null;
        }
        return support2Tree(supportList);
    }

    /**
     * 查询病历支持信息
     * @param fieldId
     * @return
     */
    public List<MedicalFieldSupport> getSupportList(Long fieldId){
        MedicalFieldSupportExample example = new MedicalFieldSupportExample();
        MedicalFieldSupportExample.Criteria criteria = example.createCriteria();
        criteria.andFieldIdEqualTo(fieldId);
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        return medicalFieldSupportMapper.selectByExample(example);
    }

    /**
     * 将数据转成树结构
     * @param supportList
     * @return
     */
    private List<MedicalFieldSupportTreeVo> support2Tree(List<MedicalFieldSupport> supportList) {
        List<MedicalFieldSupportTreeVo> medicalFieldSupportTreeVoList = new ArrayList<>();
        for (MedicalFieldSupport support: supportList) {
            MedicalFieldSupportTreeVo medicalFieldSupportTreeVo = new MedicalFieldSupportTreeVo();
            medicalFieldSupportTreeVo.setId(support.getId());
            medicalFieldSupportTreeVo.setFieldId(support.getFieldId());
            medicalFieldSupportTreeVo.setValue(support.getValue());
            medicalFieldSupportTreeVo.setText(support.getText());
            medicalFieldSupportTreeVo.setParentId(support.getParentId());
            medicalFieldSupportTreeVo.setSortNum(support.getSortNum());
            medicalFieldSupportTreeVoList.add(medicalFieldSupportTreeVo);
        }
        return TreeParser.getTreeList(CommonConstant.PARENT_ID, medicalFieldSupportTreeVoList);
    }


    /**
     * 分页查询病历字段设置列表
     * @param fieldName 字段名称（模糊查询）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    public PageInfo<MedicalField> getMedicalFieldListByCondition(String fieldName, Integer pageNo, Integer pageSize) {
        MedicalFieldExample example = new MedicalFieldExample();
        MedicalFieldExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(fieldName)) {
            criteria.andFieldNameLike("%" + fieldName + "%");
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = medicalFieldMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("sort_num asc");
        List<MedicalField> list = medicalFieldMapper.selectByExample(example);
        return new PageInfo<>(totalCount, list);
    }

    /**
     * 添加病历字段设置
     * @param medicalField 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addMedicalField(MedicalField medicalField) {
        validateMedicalField(medicalField);
        medicalField.setIsDelete(YesOrNoEnum.NO.getValue());
        medicalField.setIsEnabled(YesOrNoEnum.YES.getValue());
        medicalField.setCreateTime(new Date());
        medicalFieldMapper.insertSelective(medicalField);
    }

    /**
     * 校验病历字段参数
     * @param medicalField 实体
     */
    public void validateMedicalField(MedicalField medicalField) {
        //添加字段非空、格式等校验
        // 字段名不能为空
        if (StringUtil.isBlank(medicalField.getFieldName())) {
            throw new BusinessFailException("字段名不能为空");
        }
        // 字段编码不能为空
        if (StringUtil.isBlank(medicalField.getFieldCode())) {
            throw new BusinessFailException("字段编码不能为空");
        }
        // 字段类型不能为空
        if (StringUtil.isBlank(medicalField.getFieldType())) {
            throw new BusinessFailException("字段类型不能为空");
        }
        // 字段类型值非法
        if (!FieldTypeEnum.isValue(medicalField.getFieldType())) {
            throw new BusinessFailException("字段类型非法");
        }
        // 是否必须不能为空
        if (StringUtil.isBlank(medicalField.getIsRequired())) {
            medicalField.setIsRequired(YesOrNoEnum.NO.getValue());
        }
        // 是否必须值非法
        if (!YesOrNoEnum.isValue(medicalField.getIsRequired())) {
            throw new BusinessFailException("是否必须字段非法");
        }
        // 是否输入推荐不为空时校验值合法性
        if (StringUtil.isBlank(medicalField.getIsRecom())) {
            medicalField.setIsRecom(YesOrNoEnum.NO.getValue());
        }
        if (!YesOrNoEnum.isValue(medicalField.getIsRecom())) {
            throw new BusinessFailException("是否输入推荐字段非法");
        }
        if(StringUtil.isBlank(medicalField.getFieldScope())){
            medicalField.setFieldScope(FieldScopeEnum.ALL.getValue());
        }
        if (!FieldScopeEnum.isValue(medicalField.getFieldScope())) {
            throw new BusinessFailException("字段范围非法");
        }
        if(StringUtil.isBlank(medicalField.getFieldStyle())){
            throw new BusinessFailException("请选择字段样式");
        }
        if (!FieldStyleEnum.isValue(medicalField.getFieldStyle())) {
            throw new BusinessFailException("字段样式非法");
        }
        // 是否语音输入不为空时校验值合法性
        if (StringUtil.isBlank(medicalField.getIsVoice())) {
            medicalField.setIsVoice(YesOrNoEnum.NO.getValue());
        }
        if (!YesOrNoEnum.isValue(medicalField.getIsVoice())) {
            throw new BusinessFailException("是否语音输入字段非法");
        }
        // 是否上传图片不为空时校验值合法性
        if (StringUtil.isBlank(medicalField.getIsImage())) {
            medicalField.setIsImage(YesOrNoEnum.NO.getValue());
        }
        if (!YesOrNoEnum.isValue(medicalField.getIsImage())) {
            throw new BusinessFailException("是否上传图片字段非法");
        }
        // 是否脉象选择不为空时校验值合法性
        if (StringUtil.isBlank(medicalField.getIsPulse())) {
            medicalField.setIsPulse(YesOrNoEnum.NO.getValue());
        }
        if (!YesOrNoEnum.isValue(medicalField.getIsPulse())) {
            throw new BusinessFailException("是否脉象选择字段非法");
        }
        if(StringUtil.isBlank(medicalField.getIsDefault())){
            medicalField.setIsDefault(YesOrNoEnum.NO.getValue());
        }
        if(!YesOrNoEnum.isValue(medicalField.getIsDefault())){
            throw new BusinessFailException("是否默认字段非法");
        }
        if(StringUtil.isBlank(medicalField.getIsPrefix())){
            medicalField.setIsDefault(YesOrNoEnum.NO.getValue());
        }
        if(!YesOrNoEnum.isValue(medicalField.getIsPrefix())){
            throw new BusinessFailException("是否增加前缀非法");
        }
    }

    /**
     * 根据ID获取病历字段信息
     * @param fieldId 主键
     * @return 实体
     */
    public MedicalField getMedicalFieldById(Long fieldId) {
        if (StringUtil.isBlank(fieldId)) {
            return null;
        }
        MedicalField medicalField = medicalFieldMapper.selectByPrimaryKey(fieldId);
        if (medicalField == null || YesOrNoEnum.YES.getValue().equals(medicalField.getIsDelete())) {
            return null;
        }
        return medicalField;
    }

    public MedicalField getEnabledMedicalFieldById(Long fieldId) {
        MedicalField medicalField = getMedicalFieldById(fieldId);
        if(medicalField == null){
            return null;
        }
        if(YesOrNoEnum.NO.getValue().equals(medicalField.getIsEnabled())){
            return null;
        }
        return medicalField;
    }

    /**
     * 修改病历字段设置
     * @param medicalField 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateMedicalField(MedicalField medicalField) {
        validateMedicalField(medicalField);
        MedicalField dbMedicalField = getMedicalFieldById(medicalField.getFieldId());
        if (dbMedicalField == null) {
            throw new BusinessFailException("病历字段设置不存在");
        }
        medicalField.setUpdateTime(new Date());
        medicalFieldMapper.updateByPrimaryKeySelective(medicalField);
    }

    /**
     * 删除病历字段设置（逻辑删除）
     * @param medicalField 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMedicalField(MedicalField medicalField) {
        MedicalField dbMedicalField = getMedicalFieldById(medicalField.getFieldId());
        if (dbMedicalField == null) {
            throw new BusinessFailException("病历字段设置不存在");
        }
        dbMedicalField.setIsDelete(YesOrNoEnum.YES.getValue());
        dbMedicalField.setUpdateUserId(medicalField.getUpdateUserId());
        dbMedicalField.setUpdateBy(medicalField.getUpdateBy());
        dbMedicalField.setUpdateTime(new Date());
        medicalFieldMapper.updateByPrimaryKeySelective(dbMedicalField);
    }

    /**
     * 启用病历字段设置
     * @param medicalField 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableMedicalField(MedicalField medicalField) {
        MedicalField dbMedicalField = getMedicalFieldById(medicalField.getFieldId());
        if (dbMedicalField == null) {
            throw new BusinessFailException("病历字段设置不存在");
        }
        if (YesOrNoEnum.YES.getValue().equals(dbMedicalField.getIsEnabled())) {
            return;
        }
        dbMedicalField.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbMedicalField.setUpdateUserId(medicalField.getUpdateUserId());
        dbMedicalField.setUpdateBy(medicalField.getUpdateBy());
        dbMedicalField.setUpdateTime(new Date());
        medicalFieldMapper.updateByPrimaryKeySelective(dbMedicalField);
    }

    /**
     * 停用病历字段设置
     * @param medicalField 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableMedicalField(MedicalField medicalField) {
        MedicalField dbMedicalField = getMedicalFieldById(medicalField.getFieldId());
        if (dbMedicalField == null) {
            throw new BusinessFailException("病历字段设置不存在");
        }
        if (YesOrNoEnum.NO.getValue().equals(dbMedicalField.getIsEnabled())) {
            return;
        }
        dbMedicalField.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbMedicalField.setUpdateUserId(medicalField.getUpdateUserId());
        dbMedicalField.setUpdateBy(medicalField.getUpdateBy());
        dbMedicalField.setUpdateTime(new Date());
        medicalFieldMapper.updateByPrimaryKeySelective(dbMedicalField);
    }

    /**
     * 分页查询病历字段值列表
     * @param fieldId 字段ID（精确查询）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    public PageInfo<MedicalFieldSupport> getSupportListByCondition(Long fieldId, Integer pageNo, Integer pageSize) {
        MedicalFieldSupportExample example = new MedicalFieldSupportExample();
        MedicalFieldSupportExample.Criteria criteria = example.createCriteria();

        if (fieldId != null) {
            criteria.andFieldIdEqualTo(fieldId);
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = medicalFieldSupportMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("create_time desc");
        List<MedicalFieldSupport> list = medicalFieldSupportMapper.selectByExample(example);
        return new PageInfo<>(totalCount, list);
    }

    /**
     * 添加病历字段值
     * @param medicalFieldSupport 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addFieldSupport(MedicalFieldSupport medicalFieldSupport) {
        validateFieldSupport(medicalFieldSupport);
        medicalFieldSupport.setIsDelete(YesOrNoEnum.NO.getValue());
        medicalFieldSupport.setIsEnabled(YesOrNoEnum.YES.getValue());
        medicalFieldSupport.setCreateTime(new Date());
        medicalFieldSupportMapper.insertSelective(medicalFieldSupport);
    }

    /**
     * 校验病历字段值
     * @param medicalFieldSupport 实体
     */
    public void validateFieldSupport(MedicalFieldSupport medicalFieldSupport) {
        //补充必填字段校验，如fieldId、value内容等
        MedicalField medicalField = getMedicalFieldById(medicalFieldSupport.getFieldId());
        if(medicalField == null){
            throw new BusinessFailException("字段不存在");
        }
        if (StringUtil.isBlank(medicalFieldSupport.getValue())) {
            throw new BusinessFailException("支持VALUE不能为空");
        }
        if (StringUtil.isBlank(medicalFieldSupport.getText())) {
            throw new BusinessFailException("支持TEXT不能为空");
        }
        if(StringUtil.isBlank(medicalFieldSupport.getParentId())){
            medicalFieldSupport.setParentId(Long.valueOf(CommonConstant.DEFAULT_0));
        }else {
            MedicalFieldSupport parentMedicalFieldSupport = getFieldSupportById(medicalFieldSupport.getParentId());
            if(parentMedicalFieldSupport == null){
                throw new BusinessFailException("父层级不存在");
            }
        }
        if (StringUtil.isBlank(medicalFieldSupport.getSortNum())) {
            medicalFieldSupport.setSortNum(CommonConstant.DEFAULT_0);
        }
    }

    /**
     * 根据ID获取病历字段值详情
     * @param id 主键
     * @return 实体
     */
    public MedicalFieldSupport getFieldSupportById(Long id) {
        if (StringUtil.isBlank(id)) {
            return null;
        }
        MedicalFieldSupport fieldSupport = medicalFieldSupportMapper.selectByPrimaryKey(id);
        if (fieldSupport == null || YesOrNoEnum.YES.getValue().equals(fieldSupport.getIsDelete())) {
            return null;
        }
        return fieldSupport;
    }

    /**
     * 修改病历字段值
     * @param fieldSupport 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateFieldSupport(MedicalFieldSupport fieldSupport) {
        validateFieldSupport(fieldSupport);
        MedicalFieldSupport dbMedicalFieldSupport = getFieldSupportById(fieldSupport.getId());
        if (dbMedicalFieldSupport == null) {
            throw new BusinessFailException("病历字段值不存在");
        }
        fieldSupport.setUpdateTime(new Date());
        medicalFieldSupportMapper.updateByPrimaryKeySelective(fieldSupport);
    }

    /**
     * 删除病历字段值（逻辑删除）
     * @param medicalFieldSupport 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteFieldSupport(MedicalFieldSupport medicalFieldSupport) {
        MedicalFieldSupport dbMedicalFieldSupport = getFieldSupportById(medicalFieldSupport.getId());
        if (dbMedicalFieldSupport == null) {
            throw new BusinessFailException("病历字段值不存在");
        }
        Long childCount = getSupportCount(dbMedicalFieldSupport.getId());
        if(childCount > CommonConstant.DEFAULT_0){
            throw new BusinessFailException("存在子数据不允许删除");
        }
        dbMedicalFieldSupport.setIsDelete(YesOrNoEnum.YES.getValue());
        dbMedicalFieldSupport.setUpdateUserId(medicalFieldSupport.getUpdateUserId());
        dbMedicalFieldSupport.setUpdateBy(medicalFieldSupport.getUpdateBy());
        dbMedicalFieldSupport.setUpdateTime(new Date());
        medicalFieldSupportMapper.updateByPrimaryKeySelective(dbMedicalFieldSupport);
    }


    public Long getSupportCount(Long parentId){
        MedicalFieldSupportExample example = new MedicalFieldSupportExample();
        MedicalFieldSupportExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        return medicalFieldSupportMapper.countByExample(example);
    }

    /**
     * 启用病历字段值
     * @param medicalFieldSupport 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableFieldSupport(MedicalFieldSupport medicalFieldSupport) {
        MedicalFieldSupport dbFieldValue = getFieldSupportById(medicalFieldSupport.getId());
        if (dbFieldValue == null) {
            throw new BusinessFailException("病历字段值不存在");
        }
        if (YesOrNoEnum.YES.getValue().equals(dbFieldValue.getIsEnabled())) {
            return;
        }
        dbFieldValue.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbFieldValue.setUpdateUserId(medicalFieldSupport.getUpdateUserId());
        dbFieldValue.setUpdateBy(medicalFieldSupport.getUpdateBy());
        dbFieldValue.setUpdateTime(new Date());
        medicalFieldSupportMapper.updateByPrimaryKeySelective(dbFieldValue);
    }

    /**
     * 停用病历字段值
     * @param fieldValue 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableFieldSupport(MedicalFieldSupport fieldValue) {
        MedicalFieldSupport dbFieldValue = getFieldSupportById(fieldValue.getId());
        if (dbFieldValue == null) {
            throw new BusinessFailException("病历字段值不存在");
        }
        if (YesOrNoEnum.NO.getValue().equals(dbFieldValue.getIsEnabled())) {
            return;
        }
        dbFieldValue.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbFieldValue.setUpdateUserId(fieldValue.getUpdateUserId());
        dbFieldValue.setUpdateBy(fieldValue.getUpdateBy());
        dbFieldValue.setUpdateTime(new Date());
        medicalFieldSupportMapper.updateByPrimaryKeySelective(dbFieldValue);
    }

}
